package com.emou.controller;

import com.emou.bean.User;
import com.emou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * Created on 2021/2/1.
 *
 * @author Emou
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/dashboard")
    public String toDashBoard() {
        return "/system/dashboard";
    }


    @RequestMapping("/login")
    public String login(User user, Model model) {
        User loginUser = userService.login(user);
        if (loginUser == null) {
            //登录失败
            model.addAttribute("loginErrMsg","用户名或密码错误，请重新输入！");
            return "/loginFail";
        }else {
//            session.setAttribute("LoginUser", loginUser); //登录用户记录Session
            return "/system/dashboard";
        }
    }

    @RequestMapping("/register")
    public String  register( String username,String password){
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        userService.register(user);
        return "login";
    }


}
